<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户登录</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="userInfo.query" clearable @clear="getUsers">
            <el-button slot="append" icon="el-icon-search" @click="getUsers"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="diaShow">添加用户</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="userList" style="width: 100%" border stripe>
        <el-table-column type="index" label="编号"></el-table-column>
        <el-table-column prop="username" label="姓名"></el-table-column>
        <el-table-column prop="email" label="邮箱"></el-table-column>
        <el-table-column prop="mobile" label="电话"></el-table-column>
        <el-table-column prop="role_name" label="角色"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="chageSwitch(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="分配角色">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" circle @click="EditShow(scope.row.id)"></el-button>
            <el-button type="success" icon="el-icon-delete" circle @click="deleted(scope.row.id)"></el-button>
            <el-tooltip
              class="item"
              effect="dark"
              content="分配角色"
              placement="top"
              @click="jiaose(scope.row.id)"
            >
              <el-button type="info" icon="el-icon-message" circle @click="RoleDialog(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="userInfo.pagenum"
        :page-sizes="[1, 5, 10, 20]"
        :page-size="userInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <DiaLog :showDig="showDia" @aaa="closeDia" @shuaxin="getUsers"></DiaLog>
    <EditLog :showDigs="showDias" :ids="id" @edit="closeDias" @shuaxin="getUsers"></EditLog>
    <RoleLog :showRole="RoleDia" @closeRole="closeRoles" :datas="roleList">
    </RoleLog>
  </div>
</template>

<script>
import Dialog from "./Dialog";
import Editlog from "./Editlog";
import Rolelog from "./UserDialog/RoleDialog";
export default {
  data() {
    return {
      userInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      userList: [],
      total: 0,
      showDia: "",
      showDias: "",
      id: "",
      RoleDia: false,
      roleList:''
    };
  },
  created() {
    this.getUsers();
  },
  methods: {
    getUsers() {
      this.$axios.get("users", { params: this.userInfo }).then((res) => {
        if (res.data.meta.status != 200)
          return this.$message.error("获取用户列表失败");
        console.log(res.data.data);
        this.userList = res.data.data.users;
        this.total = res.data.data.total;
      });
    },
    handleSizeChange(newSize) {
      this.userInfo.pagesize = newSize;
      this.getUsers();
    },
    handleCurrentChange(val) {
      this.userInfo.pagenum = val;
      this.getUsers();
    },
    chageSwitch(a) {
      this.$axios.put(`users/${a.id}/state/${a.mg_state}`).then((res) => {
        if (res.data.meta.status !== 200) {
          a.mg_state = !a.mg_state;
          return this.$message.error("更新用户状态失败");
        }
        this.$message.success("更新用户状态成功");
      });
    },
    diaShow() {
      this.showDia = true;
    },
    closeDia() {
      this.showDia = false;
    },
    closeDias() {
      this.showDias = false;
    },
    EditShow(id) {
      this.showDias = true;
      this.id = id;
    },
    //删除
    deleted(id) {
      this.$messageBox("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios.delete("/users/" + id).then((res) => {
            console.log(res);
            if (res.data.meta.status == 200) {
              this.$message({
                message: res.data.meta.msg,
                type: "success",
              });
              this.getUsers();
            } else {
              this.$message({
                message: "删除失败",
                type: "error",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //更新用户角色
    jiaose(id) {},
    //分配角色
    RoleDialog(data) {
      console.log(data)
      this.roleList=data;
      this.RoleDia = true;
    },
    closeRoles(){
      this.RoleDia = false;
    }
  },
  components: {
    DiaLog: Dialog,
    EditLog: Editlog,
    RoleLog:Rolelog
  },
};
</script>
<style lang='scss' scoped>
</style>